package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.po.Notice;
import com.ChenYaRou.www.po.Role;
import com.ChenYaRou.www.po.Shop;
import com.ChenYaRou.www.utils.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.*;
import static com.ChenYaRou.www.constant.False.EMPTY;

/**
 * @ClassName CreatShopServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 17:52
 * @Version 1.0
 **/
public class CreatShopServletController {

    /**
     * 申请时先在shop insert进去 但state为暂停营业状态 然后发送notice给管理员
     * @param
     * @return
     */
    public void theApplyCreateShop(HttpServletRequest request, HttpServletResponse response){
        String ownerId = request.getParameter("ownerId");
        String name = request.getParameter("name");
        String introduction = request.getParameter("introduction");
        String district = request.getParameter("district");
        String street = request.getParameter("street");
        String detailedAddress = request.getParameter("detailedAddress");
        if(ownerId!=null && name!=null && introduction!=null &&district!=null && street!=null &&detailedAddress!=null){
            Shop shop=new Shop(name,introduction,0F,Integer.parseInt(ownerId),district,street,detailedAddress,CLOSE);
            shop.setState(CLOSE);
            try {
                new ShopServletController().makePicture(request,shop);
                String sqlInsertShop=GenerateSql.getInstance().insert(shop);
                Role oldRole=new Role(shop.getOwnerName(),USER);
                String sqlUpdateRole=GenerateSql.getInstance().update(new Role(shop.getOwnerName(),SELLER),oldRole);

                Notice notice=new Notice(APPLY_CREATE_SHOP+"["+shop.out()+"]", LocalDateTime.now(),
                        NEED, Integer.parseInt(ownerId));
                String sqlInsertNotice=GenerateSql.getInstance().insert(notice);
                int i = JdbcUtil.getInstance().insertOrUpdateMore(sqlInsertShop, sqlUpdateRole, sqlInsertNotice);
                if(i>0){
                    response.sendRedirect("index.jsp?state=SUCCESS");
                }else{
                    response.sendRedirect("index.jsp?state=fail");
                }
            } catch (ServletException | IOException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.sendRedirect("index.jsp?state=incomplete information");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查找创建时的名字是否唯一
     * @param shopName
     * @return
     */
    public String theSameShopName(String shopName){
        if(shopName!=null && !EMPTY.equals(shopName.trim())){
            String sensitiveFilter = SensitiveFilter.getInstance().sensitiveFilter(shopName);
            if(sensitiveFilter!=null){
                return sensitiveFilter;
            }
            Shop shop=new Shop();
            shop.setName(shopName);
            List<Shop> shops = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(shop);
            if(shops!=null && !shops.isEmpty()){
                return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SAME_NAME,null);
            }
            return JsonBean.operateList(JsonBeanConstant.NULL,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }
}
